import type { XYCoord } from '../classes/interface';

const ELEMENT_NODE = 1; //元素节点

// 元素
export function getNodeClientOffset(node: HTMLElement): XYCoord | undefined {
  const el = node.nodeType === ELEMENT_NODE ? node : node.parentElement;

  if (!el) {
    return undefined;
  }

  const { top, left } = (el as HTMLElement).getBoundingClientRect();
  return { x: left, y: top };
}

// 鼠标
export function getEventClientOffset(e: DragEvent): XYCoord {
  return {
    x: e.clientX,
    y: e.clientY,
  };
}
